package cn.shlbysj.demo.controller;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.DownloadInfo;
import cn.shlbysj.demo.service.IArticleService;
import cn.shlbysj.demo.service.IDownloadInfoService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/download")
@Api("免费下载模块")
public class DownloadInfoController {

    @Autowired
    private IDownloadInfoService downloadInfoService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取免费下载列表")
    @ResponseBody
    public Result list(HttpServletRequest request,
                       @ApiParam(name="title",value="标题")@RequestParam(value = "title",required = false) String title,
                       @ApiParam(name="page",value="当前页",required = true)@RequestParam(value = "page",required = true) Integer page ,
                       @ApiParam(name="rows",value="每页显示多少条",required = true)@RequestParam(value = "rows",required = true) Integer rows){
        try {
            PageResult pageResult = downloadInfoService.findPage(title,page,rows);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(pageResult),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "添加免费下载")
    @ResponseBody
    public Result insert(HttpServletRequest request,
                         @ApiParam(name="downloadInfo",value="毕设信息")@RequestBody DownloadInfo downloadInfo
    ){
        try {
            return downloadInfoService.insert(downloadInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(httpMethod = "DELETE", value = "删除毕设")
    @ResponseBody
    public Result delete(HttpServletRequest request,
                         @ApiParam(name="dId",value="主键ID",required = true)@RequestParam String dId
    ){
        try {
            return downloadInfoService.delete(dId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
}
